﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Enumerations.AddPoint_Enum;
using System.Data;
public partial class Master_Admin_AddPoint : Admin_BasePage
{
    protected AddPoint pointClass = new AddPoint();
    protected string action = "";
    protected string whereStr = " IsDel=0";
    protected void Page_Load(object sender, EventArgs e)
    {

        IsExit();
        if (!string.IsNullOrEmpty(Request["action"]))
        {
            action = Request["action"].ToString();
        }
        plVisible();
        if (!Page.IsPostBack)
        {
            bind();
        }
    }
    /// <summary>
    /// 参数不同，隐藏不同的Panel
    /// </summary>
    protected void plVisible()
    {
        switch (action)
        {
            case "addmore":
                this.Pl_Add.Visible = true;
                this.Pl_Manager.Visible = false;

                break;
            default:
                this.Pl_Add.Visible = false;
                this.Pl_Manager.Visible = true;
                break;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int CreateNum = StringHelper.isNumber_int(CardNum.Text.ToString());
        int AddPoint = StringHelper.isNumber_int(point.Text.ToString());
        decimal AgentMoney = StringHelper.isNumber_decimal(AgentPrice.Text.ToString());
        decimal MemberMoney = StringHelper.isNumber_decimal(MemberPrice.Text.ToString());
        for (int i = 1; i <= CreateNum; i++)
        {
            string RndNum = StringHelper.GenerateRandom(6) + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
            string Rndpwd = StringHelper.GenerateRandom(8);

            AddPoint_Model model = new AddPoint_Model();
            model.CardNum = RndNum;
            model.CardPwd = Rndpwd;
            model.MemberPrice = MemberMoney;
            model.AgentPrice = AgentMoney;
            model.Point = AddPoint;
            model.State = (int)AspNetCs.Enumerations.AddPoint_Enum.Point_state.NoUse;
            model.UseType = (int)AspNetCs.Enumerations.AddPoint_Enum.UseObj.Unknown;
            pointClass.Add(model);
        }
        ResultStr("Admin_AddPoint.Aspx", "充值卡增加成功！");
    }

    protected void bind()
    {
        Pager p = new Pager();
        p.Obj = Repeater1;
        p.TableName = "AddPoint";
        p.WhereStr = whereStr;
        p.Orderfld = "id";
        p.PageSize = this.AspNetPager1.PageSize;
        p.PageIndex = this.AspNetPager1.CurrentPageIndex;
        p.OrderType = "desc";
        p.ControlBind();

        this.AspNetPager1.RecordCount = p.recordcount();

    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton linkdel = e.Item.FindControl("linkdel") as LinkButton;
        int id = StringHelper.isNumber_int(e.CommandArgument.ToString());
        if (e.CommandName == "delete")
        {
            if (linkdel != null)
            {
                int state = 1;
                string cmdText = "update AddPoint set IsDel={0} where id={1}";
                cmdText = string.Format(cmdText, state, id);
                ExcuteSqlTextBLL.ExcuteResult(cmdText);
            }
        }
        else if (e.CommandName == "priceuse")
        {
            Response.Redirect("AdminOnlinAddPoint.aspx?id=" + id);
        }
        bind(); 
        
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemIndex != -1)
        {

            LinkButton lbtn = e.Item.FindControl("linkbtn") as LinkButton;
            DataRowView drv = (DataRowView)e.Item.DataItem;
            if (StringHelper.isNumber_int(drv["state"].ToString()) == ((int)Point_state.YesUse))
            {
                lbtn.Text = "已使用";
                lbtn.Enabled = false;
            }
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bind();
    }
}
